<?php

namespace app\common\library;

use think\Config;
use Tx\Mailer;
use Tx\Mailer\Exceptions\CodeException;
use Tx\Mailer\Exceptions\SendException;

/**
 * 随附达三方支付
 * @classname SfdPay
 * @package app\common\library
 */
class SfdPay
{
    /**
     * POST请求
     * @param {Object} $post_url
     * @param {Object} $post_data
     */
    public function curlPost($post_url, $post_data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $post_url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        return curl_exec($ch);
    }

    /**
     * 私钥签名
     *
     * @param  array  $arr  参数
     * @return string
     */
    public function sign($arr)
    {
        unset($arr['sign']);
        $str = $this->signQueryString($arr); // 参数处理
        $pk = '-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDFEut5FobFaXY9
KZoioiTiJOp1ADNRCHFzhmirCg0MnvIq8cNNbNtSBHw2ROiZDAkcltZsANMoCMAK
NbJS3rB2SWc1aA6kwv60OzqkvqBDQzb4UnpMxdWsG4e356SRvoNOm6AhVz/7Bo+2
/vCIKT8OFG2u2sWBcFaE024nfr1sMM4uFO3GsZtJxlZNcBhyGnVkb7qFkdiO2KvV
YT42nff12QrpJgUUgbAp4SDMtBF/zYrm2ULsjRq/mLoocYNw1b9CPOTMbu4q1GYQ
cPSX9yEHq8FklfHd4STdLpMOwLecWNsCHP70TlBfe/ZR6Ndc6kvmHp0tHa1tGRrB
sBNYBZnlAgMBAAECggEAaDNkxhWfFOp6VkSU9xL8pYYXYd3FRGdOdWLwHN+nnrC8
IvDJ7zpLjMZKVveAdMvkVXV70odtHpHh2IQj0aVlcGXHaHmopH1l5y4aB9mzK11y
x0H04HsKS4IS9K5KWbV+ACZROM/FSJlji8Kos5X7fRpYFEggfhxD4t9YMxHUXktQ
lJ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key = openssl_pkey_get_private($pk);
        openssl_sign($str, $signature, $pi_key,OPENSSL_ALGO_SHA1);
        unset($pi_key);
        return base64_encode($signature);
    }

    /**
     * 参数处理
     *
     * @param  array  $params
     * @return string
     */
    public function signQueryString($params)
    {
        ksort($params);
        $qb_str = '';
        foreach ($params as $key => $val) {
            if (!empty($val)) {
                $qb_str .= $key . '=' . $val . '&';
            }
        }
        return rtrim($qb_str, '&');
    }

    /**
     * 公钥验证
     *
     * @param  array   $arr   参数
     * @param  string  $sign  签名
     * @return int
     */
    function verify($arr, $sign)
    {
        unset($arr['sign']);
        $str = $this->signQueryString($arr); // 参数处理
        $pu_key = openssl_pkey_get_public('-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxRLreRaGxWl2PSmaIqIk
4iTqdQAzUQhxc4ZoqwoNDJ7yKvHDTWzbUgR8NkTomQwJHJbWbADTKAjACjWyUt6w
dklnNWgOpML+tDs6pL6gQ0M2+FJ6TMXVrBuHt+ekkb6DTpugIVc/+waPtv7wiCk/
DhRtrtrFgXBWhNNuJ369bDDOLhTtxrGbScZWTXAYchp1ZG+6hZHYjtir1WE+Np33
9dkK6SYFFIGwKeEgzLQRf82K5tlC7I0av5i6KHGDcNW/QjzkzG7uKtRmEHD0l/ch
B6vBZJXx3eEk3S6TDsC3nFjbAhz+9E5QX3v2UejXXOpL5h6dLR2tbRkawbATWAWZ
5QIDAQAB
-----END PUBLIC KEY-----');
        $flg = openssl_verify($str, base64_decode($sign), $pu_key,OPENSSL_ALGO_SHA1);
        unset($pu_key);
        return $flg;
    }

}
